package com.start.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.cms.entity.StartCmsDiyModelEntity;
import com.start.cms.service.StartCmsDiyModelService;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.entity.StartBaseEntity;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "数据模型管理")
@RequestMapping("cms/dataModel")
@RegisterMenu(value = "数据模型管理", systemCode = "start:cms:dataModel", component = "views/cms/dataModel/index", path = "/dataModel")
public class StartCmsDiyModelController {

    private static final String LOG_TITLE = "数据模型管理" ;

    @Autowired
    private StartCmsDiyModelService cmsDiyModelService;

    @GetMapping("/page")
    public Result<?> page(Page<StartCmsDiyModelEntity> page) {
        LambdaQueryWrapper<StartCmsDiyModelEntity> lambda = DataQueryConstructor.createEntity(StartCmsDiyModelEntity.class).lambda();
        lambda.orderByDesc(StartBaseEntity::getCreateTime);
        return Result.ok(cmsDiyModelService.page(page, lambda));
    }

    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "新增")
    @Operation(summary = LOG_TITLE, description = "新增")
    @PreAuthorize("@pas.hasPermission('start:cms:dataModel:add')")
    @RegisterPermission(value = "start:cms:dataModel:add", name = "添加")
    public Result<?> add(StartCmsDiyModelEntity entity) {
        cmsDiyModelService.save(entity);
        return Result.ok();
    }

    @PostMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @Operation(summary = LOG_TITLE, description = "修改")
    @PreAuthorize("@pas.hasPermission('start:cms:dataModel:edit')")
    @RegisterPermission(value = "start:cms:dataModel:edit", name = "修改")
    public Result<?> updateById(StartCmsDiyModelEntity entity) {
        cmsDiyModelService.updateById(entity);
        return Result.ok();
    }

    @PostMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @Operation(summary = LOG_TITLE, description = "删除")
    @PreAuthorize("@pas.hasPermission('start:cms:dataModel:del')")
    @RegisterPermission(value = "start:cms:dataModel:del", name = "删除")
    public Result<?> deleteById(@PathVariable("id") String id) {
        cmsDiyModelService.removeById(id);
        return Result.ok();
    }

}
